# @Time : 2020/7/28 17:35
# @Author : Fioman 
# @Phone : 13149920693
import cv2 as cv
import numpy as np

img = cv.imread("contours2.jpg")[5:230,5:280]
print(img.shape)
cv.imshow("Original",img)

gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)

T,thresOtsu =  cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
image,contours,hierarchy = cv.findContours(thresOtsu,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
n = len(contours)
contoursImg = []

for i in range(n):
    temp = np.zeros(img.shape,np.uint8)
    contoursImg.append(temp)
    contoursImg[i] = cv.drawContours(contoursImg[i],contours,i,(255,255,255),4)
    cv.imshow("contours[{}]".format(i),contoursImg[i])
    cv.waitKey(0)

cv.destroyAllWindows()